﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using PMKB_Repository;
using PMKB_Web.pluginsCode;
using System.IO;
using RazorGenerator.Mvc;
using System.Web.WebPages;
using Plugin_Interfaces;

namespace PMKB_Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
           // routes.MapRoute(
           //    "plugins", // Route name
           //    "plugins/{controller}/{action}/{id}", // URL with parameters
           //    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
           //);
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }
        
        protected void Application_Start()
        {
            string PathPlugins=Server.MapPath("~/plugins");
            var pluginController = new PluginControllerFactory(PathPlugins);

            ControllerBuilder.Current.SetControllerFactory(pluginController);
            //var PluginsName = pluginController.Controllers.ToDictionary<string, MVCPLugin>(item =>null
            //    //item => item.Key.ToLowerInvariant().Replace("controller","")).ToArray();
            //);
            //Application["Plugins"] = pluginController.Controllers;
            SiteUtils.plugins = PluginControllerFactory.Controllers;
            
            AreaRegistration.RegisterAllAreas();
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new RazorViewEngine());
            var engine = new ViewEngineProvider(PathPlugins);
            
            ViewEngines.Engines.Add(engine);
            VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            RegisterBinders();
            //new PMKB_Web.SiteUtils.LogEventAppHarbor("before using connection string").Raise();
            InitialData.InsertTags(SiteUtils.ConnectionString);
            //new PMKB_Web.SiteUtils.LogEventAppHarbor("after using connection string").Raise();
            
        }

        private void RegisterBinders()
        {
            var imb=new Interface_ModelBinderProvider();
            imb.AddInterface<PMKB_DAL.Link, PMKB_DAL.I_Link>();

            ModelBinderProviders.BinderProviders.Add(imb);
        }
    }
}